<?php

use yii\helpers\Html;
use yii\widgets\DetailView;
use kartik\file\FileInput;
use kartik\editable\Editable;

$this->title = $model->name;
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Finances'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;

$orders = $model->order;
?>
<style type="text/css">
    .widget-box  {border: 1px solid #CCC; color: #393939;}
    .widget-box .widget-header {color: #FFF;background:#333;padding:15px 0px;text-align:center;}
    .widget-box  .color2 {background:#b34e93;}
    .widget-box .widget-body {padding:5px 15px;margin-top:15px;font-size: 13px;font-family: 'Open Sans','Microsoft Yahei';line-height:1.8}
    .widget-box .widget-body .price {text-align: center;font-size: 16px;font-weight:bold;color:#000;}
</style>
<?=  yii\web\View::registerCssFile("/css/colorbox.css" ,['depends' => 'app\assets\AppAsset']) ?>
<?=  yii\web\View::registerJsFile("/js/jquery.colorbox-min.js" ,['depends' => 'app\assets\AppAsset']) ?>

<div class="row">
    <!-- p></?= Html::a(Yii::t('app', 'Update'), ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?></p -->

	<div class="col-md-6">
    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'code',
            [
                'attribute' => 'type',
                'value' => Yii::$app->params['finance_type'][$model->type],
            ],
            [
                'attribute' => 'price_type',
                'value' => Yii::$app->params['finance_price_type'][$model->price_type],
            ],
            'company',
            'account',
            'name',
            //'desc',
            //'img',
            [
                'attribute' => 'status',
                'value' => Yii::$app->params['finance_status'][$model->status],
            ],
            //'user_id',
            'created_at',
            'updated_at:relativetime',
            'user.profile.name',
        ],
    ]) ?>
    <table class="table table-striped table-bordered detail-view">
       <tbody>
           <tr>
              <th>备注</th>
              <td>
              <?php 
              if($model->user_id == \yii::$app->user->id){
                  echo  Editable::widget([
                         'name' => 'Finance[desc]',
                        'value' => $model->desc,
                          'asPopover' => true,
                          'header' => Yii::t('app.finance', 'Desc'),
                        'inputType' => Editable::INPUT_TEXTAREA,
                          'size'=>'md',
                          'formOptions' => ['action'=>'/finance/finance/edit?id='.$model->id, 'method'=>'get'],
                          'options' => ['class'=>'form-control', 'placeholder'=> Yii::t('app.finance', 'Desc')],
                        ]);
               }else{
                 echo $model->desc;
               }
               ?>
              </td>
          </tr> 
       </tbody>
    </table>  
    
	</div>

	<div class="col-md-6">
    <?php
        echo FileInput::widget([
                       'language' => 'zh',
                       'name' => 'FinanceImg[img]',
                       'id' => 'file-upload',
                       'options' => ['accept' => '', 'multiple'=>true],
                       'pluginOptions' => [
                            'showUpload'  => false,
                            'initialPreview'=> [],
                            'initialPreviewConfig' => [],
                            'showRemove'=> false,
                            'uploadAsync'=> true,
                            'uploadUrl'=>\yii\helpers\Url::to(['/site/finance', 'id'=>$model->id]),
                            'maxFileSize' => 8120,
                            'maxFileCount' => 20,
                       ]
        ]); 
    ?>
		<div class="thumbnails" style="margin-top:30px;">
		<?php 
			if($model->allImg){
				foreach($model->allImg as $img){
                    echo  '<a data-rel="colorbox" class="cboxElement" href="/'. $img->img .'" >';
					echo  \yii\helpers\Html::img('/'.$img->img, ['width'=>90, 'height'=> 100, 'style'=>'margin-right:10px;']);

                    echo '</a>';
				}
			}
		?>
		</div>
	</div>


	<div class="text-danger col-md-12">
		<h4><b>总金额 : <font id="total-price"><?= Yii::$app->formatter->asCurrency($model->orderPrice) ?></font></b></h4> 
	</div>

	<div class="col-md-12">
	<?php if(empty($orders)) { ?>
		<div class="panel panel-body">
			<h3><small><a href="/finance/finance/order?id=<?= $model->id ?>" >尚未关联订单 ,  马上设置</a></small></h3>
		</div>
	<?php }else{ ?> 
		<h3 class="page-header"><small>关联的订单</small>
			<a class="pull-right btn btn-warning" href="/finance/finance/finance-check?id=<?= $model->id ?>" >前往审批</a>
		</h3>
		<ul class="list-group">
			<!-- 关联订单 -->
			<?php foreach($orders as $order){ ?>
				<li class="list-group-item">
					<font style="display:inline-block;width:120px;"><?= $order->order->product->code ?></font>
					<a  style="display:inline-block;width:140px;" href="/order/order/view?id=<?= $order->order_id?>" target="_blank" ><?= $order->order->code ?></a>
					<font style="display:inline-block;width:100px;"><?= $order->order->username ?></font>
					<font style="display:inline-block;width:120px;"><?= $order->order->phone ?></font>
					<font style="display:inline-block;width:120px;">
						<!-- ?= $order->price? -->
						<?php 
							if($model->status == 1 || $model->status == 4 || $model->status == 5){
								echo Editable::widget([
    		    					'name' => 'Order[price]',
									'value' => $order->price,
									'displayValue' => Yii::$app->formatter->asCurrency($order->price),
    		    					'asPopover' => true,
    		    					'header' => '修改审批金额',
    		    					'size'=>'md',
    		    					'formOptions' => ['action'=>'/finance/finance/edit-price?id='.$order->id, 'method'=>'get'],
    		    					'options' => ['class'=>'form-control', 'placeholder'=>'修改金额'],
    		    					'pluginEvents'=> [
    		    					     "editableSuccess"=>"function(event, val, form, data) { 
											$('#total-price').text(data.total); // 更改订单总金额显示
    		    					     }",
    		    					],
    							]);
							}else{
								echo Yii::$app->formatter->asCurrency($order->price);	
							}
						?>
					</font>
				</li>
			<?php } ?>
		</ul>
	<?php } ?>
	</div>

    <div class="col-md-12" style="padding-bottom:15px;">
       
      
            <span style="font-size: 16px;color: #777;font-weight: bold;margin-right: 15px;">是否开启发票</span>
            <?= Editable::widget([
                    'name' => 'Finance[is_invoice]',
                    'value' => Yii::$app->params['is_invoice'][$model->is_invoice],
                    'asPopover' => true,
                    'header' => '类型',
                    'inputType' => Editable::INPUT_DROPDOWN_LIST,
                    'data'=> Yii::$app->params['is_invoice'],
                    'size'=>'md',
                    'formOptions' => ['action'=>'/finance/finance/edit?id='.$model->id, 'method'=>'get'],
                    'options' => ['class'=>'form-control', 'placeholder'=>'请选择类型'],
                    'pluginEvents'=> [
                        "editableSuccess"=>"function(event, val, form, data) { 
                             window.location.reload(); }",
                    ],
                ])
            ?>
    
        
    </div>

    <?php if($orders && $model->is_invoice == 2 && $model->type <=2) { ?>

        <?=$this->render('invoice', ['model' => $model]);?>
    
    <?php }?>
   

</div>

<div class="modal fade invoice-modal" role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog " role="document" style="width:1060px;">
      <div class="modal-content">
            <!-- 嵌入内容 --> 
      </div>
  </div>
</div>

<?php app\components\JsBlock::begin() ?>
<script type="text/javascript">  
    /** 图片自动上传 */
    $('#file-upload').on('filebatchselected', function(event, files) {
        $(this).fileinput("upload");
    });

    $('#file-upload').on('fileuploaded', function(event, data, previewId, index) {
        var form = data.form, files = data.files, extra = data.extra,response = data.response, reader = data.reader;
    });


     // 清除模态框缓存
    $(".invoice-modal").on("hidden.bs.modal", function() {
        $(this).removeData("bs.modal");
        window.location.reload();
    });

    var colorbox_params = {
         rel: 'colorbox',
         scrolling:false,
         close:'&times;',
         maxWidth:'100%',
         maxHeight:'100%',
         onOpen:function(){
             $overflow = document.body.style.overflow;
             document.body.style.overflow = 'hidden';
         },
         onClosed:function(){
             document.body.style.overflow = $overflow;
         },
         onComplete:function(){
           $.colorbox.resize();
         }
    };

    $('.thumbnails [data-rel="colorbox"]').colorbox(colorbox_params);

</script>
<?php app\components\JsBlock::end() ?>
